package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.domain.entity.*;
import org.example.domain.model.ProductAllInfo;
import org.example.domain.model.ProductCommentsModel;
import org.example.mapper.*;
import org.example.service.IProductParticularsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductParticularsImpl implements IProductParticularsService {

    //设置了图片的访问地址
    private static final String url="https://fzlbkt.oss-cn-hangzhou.aliyuncs.com/exampledir/";

    private ProductIdMapper productIdMapper;
    private LoginNameMapper loginNameMapper;
    private ProductCommentsMapper productCommentsMapper;
    private ProductImgMapper productImgMapper;
    private ProductParamsMapper productParamsMapper;
    private ProductSkuMapper productSkuMapper;


    public ProductParticularsImpl(ProductIdMapper productIdMapper, LoginNameMapper loginNameMapper, ProductCommentsMapper productCommentsMapper, ProductImgMapper productImgMapper, ProductParamsMapper productParamsMapper, ProductSkuMapper productSkuMapper) {
        this.productIdMapper = productIdMapper;
        this.loginNameMapper = loginNameMapper;
        this.productCommentsMapper = productCommentsMapper;
        this.productImgMapper = productImgMapper;
        this.productParamsMapper = productParamsMapper;
        this.productSkuMapper = productSkuMapper;
    }

    //商品详情页面的数据查询
    public ProductAllInfo selectProductAllInfo(Integer productId){
        //设计相应的mapper进行修饰
        QueryWrapper<ProductId> productIdQueryWrapper = new QueryWrapper<>();
        productIdQueryWrapper.eq("product_id",productId);
        QueryWrapper<ProductComments> productCommentsQueryWrapper = new QueryWrapper<>();
        productCommentsQueryWrapper.eq("product_id",productId);
        QueryWrapper<ProductImg> productImgQueryWrapper = new QueryWrapper<>();
        productImgQueryWrapper.eq("item_id",productId);
        QueryWrapper<ProductParams> productParamsQueryWrapper = new QueryWrapper<>();
        productParamsQueryWrapper.eq("product_id",productId);
        QueryWrapper<ProductSku> productSkuQueryWrapper = new QueryWrapper<>();
        productSkuQueryWrapper.eq("product_id",productId);

        //避免返回给后端的对象中存在null的情况
        ProductAllInfo productAllInfo = new ProductAllInfo();
        productAllInfo.setProductId(productIdMapper.selectOne(productIdQueryWrapper)==null ? new ProductId() : productIdMapper.selectOne(productIdQueryWrapper));
        List<ProductComments> productComments = productCommentsMapper.selectList(productCommentsQueryWrapper);
        productComments.add(new ProductComments());
        productAllInfo.setProductCommentsList(productComments);
        List<ProductImg> productImgs = productImgMapper.selectList(productImgQueryWrapper);
        productImgs.forEach(temp -> {
            temp.setUrl(url + temp.getUrl());
        });
        if(productImgs.size() == 0){
            ProductImg productImg = new ProductImg();
            productImg.setUrl(url + "errorImg.jpg");
            productImgs.add(productImg);
        }
        productAllInfo.setProductImgList(productImgs);
        productAllInfo.setProductParams(productParamsMapper.selectOne(productParamsQueryWrapper)==null ? new ProductParams() : productParamsMapper.selectOne(productParamsQueryWrapper));
        List<ProductSku> productSkus = productSkuMapper.selectList(productSkuQueryWrapper);
        productSkus.add(new ProductSku());
        productAllInfo.setProductSkuList(productSkus);
        return productAllInfo;
    }

    //查询商品的参数信息（比如产品等参数信息）
    public ProductParams selectProductParamsById(Integer productId){
        QueryWrapper<ProductParams> productParamsQueryWrapper = new QueryWrapper<>();
        productParamsQueryWrapper.eq("product_id",productId);
        return  productParamsMapper.selectOne(productParamsQueryWrapper);
    }

    @Override
    public List<ProductComments> selectProductCommentsById(Integer productId) {
        QueryWrapper<ProductComments> productCommentsQueryWrapper = new QueryWrapper<>();
        productCommentsQueryWrapper.eq("product_id",productId);
        List<ProductComments> productComments = productCommentsMapper.selectList(productCommentsQueryWrapper);
        productComments.forEach(pco -> {
            QueryWrapper<UserName> userNameQueryWrapper = new QueryWrapper<>();
            userNameQueryWrapper.eq("user_id",pco.getUserId());
            UserName userName1 = loginNameMapper.selectById(pco.getUserId());
            UserName userName = loginNameMapper.selectOne(userNameQueryWrapper);

            pco.setUserName(userName1.getUsername());
        });

        return productComments;
    }

}
